//
//  XMN_QuiteViewController.m
//  B段项目
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "XMN_QuiteViewController.h"
#import "Header.h"
#import "Music.h"

@interface XMN_QuiteViewController ()
@property(nonatomic, strong)UIView *backView;
@property(nonatomic, assign)NSInteger time;
@property(nonatomic, strong)UILabel *timeLabel;
@property(nonatomic, strong)NSTimer *T;
@property(nonatomic, strong)NSString *musicString;
@property(nonatomic, strong)NSMutableArray *musicArray;
@property(nonatomic, strong)UIImageView *musicImageView;

@property(nonatomic, strong)UIImageView *imageView1;
@property(nonatomic, strong)UILabel *label;
@end

@implementation XMN_QuiteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _musicString = @"http://pkicdn.image.alimmdn.com/old/newuploads/7d3397df32528b0c3307dcb94c12f10a.mp3";
    _time = 80;
    self.view.backgroundColor = [UIColor whiteColor];
    
    _backView = [[UIView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:_backView];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:_backView.frame];
    UIImage *backImage = [[UIImage imageNamed:@"XMN_Quite猫"] boxblurImageWithBlur:15];
    imageView.image = backImage;
    [_backView addSubview:imageView];
    
    
    UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(100 * FitWidth, 80 * FitHeight, ScreenWidth - 180 * FitWidth, 60 * FitHeight)];
    contentLabel.font = [UIFont systemFontOfSize:15 * FitWidth];
    contentLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:contentLabel];
    contentLabel.numberOfLines = 0;
    contentLabel.text = @"        世界很吵,戴上耳机 \n               感受自己的世界...";
    [imageView addSubview:contentLabel];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(325 * FitWidth, 570 * FitHeight, 40 * FitWidth, 40 * FitWidth);
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 20 * FitWidth;
    button.backgroundColor = [UIColor colorWithRed:0.74 green:0.74 blue:0.74 alpha:.6];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * FitWidth, 80 * FitHeight, 80 * FitWidth, 40 * FitHeight)];
    _timeLabel.text = @"80s'";
    _timeLabel.font = [UIFont systemFontOfSize:30 * FitWidth];
    _timeLabel.textColor = [UIColor whiteColor];
    [imageView addSubview:_timeLabel];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(_timeLabel.frame.origin.x + _timeLabel.frame.size.width, _timeLabel.frame.origin.y, 2 * FitWidth, 40 * FitHeight)];
    label.backgroundColor = [UIColor whiteColor];
    [imageView addSubview:label];

    _T = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];

    _imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(340 * FitWidth, 90 * FitHeight, 30 *FitWidth, 30 * FitHeight)];
    _imageView1.image = [UIImage imageNamed:@"XMN_1"];
    [imageView addSubview:_imageView1];
    
    
}

-(void)viewDidAppear:(BOOL)animated{
    [[Music shareInstance] playMusicWith:_musicString];
}


-(void)timerAction{
    
    if (_time % 4 == 0) {
        NSArray *array = [NSArray arrayWithObjects:@"  ❀", @"❀", @"  ❀", @"❀", @"   ❀", @"❀     ", nil];
        for (NSInteger i = 0; i < 20; i++) {
            _label = [[UILabel alloc]initWithFrame:CGRectMake(20 * FitWidth + i * 40 * FitWidth, -200 * FitHeight, 200 * FitWidth, 20)];
            NSInteger n = arc4random() % array.count;
            _label.text = [array objectAtIndex:n];
            _label.textAlignment = 0;
            _label.textColor = [UIColor whiteColor];
            _label.font = [UIFont systemFontOfSize:20 * FitWidth];
            CGRect frame = _label.frame;
            frame.origin.y = 0;
            _label.frame = frame;
            [UIView beginAnimations:@"一条弹幕效果" context:nil];
            [UIView setAnimationDuration:arc4random() % (20 - 5 + 1) + 5];
            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
            [UIView setAnimationRepeatCount:100];
            frame = _label.frame;
            frame.origin.y = ScreenHeight;
            _label.frame = frame;
            [UIView commitAnimations];
            [self.view addSubview:_label];
        }
    }
    
    if (_time == 0) {
        [_T invalidate];
        _T = nil;
        [[Music shareInstance] pauseMusicWith:_musicString];
        DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"是否要离开静静" contentText:@"不要%>_<%" leftButtonTitle:@"静静" rightButtonTitle:@"离开"];
        [alert show];
        alert.rightBlock = ^(){
            [self buttonAction];
        };
        alert.leftBlock = ^(){
            _time = 80;
            _T = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
            [[Music shareInstance] playMusicWith:_musicString];
        };
    }else{
        _time--;
        _timeLabel.text = [NSString stringWithFormat:@"%lds'", (long)_time];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)buttonAction{
    [_T invalidate];
    _T = nil;
    [[Music shareInstance] pauseMusicWith:_musicString];
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
